package com.ruoyi.project.cms.task.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.task.domain.Task;
import com.ruoyi.project.cms.task.service.ITaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 任务信息
 */
@Controller
@RequestMapping("/cms/task")
public class TaskController extends BaseController {
    private String prefix = "cms/task";
    @Autowired
    private ITaskService taskService;
    @RequiresPermissions("cms:task:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/task";
    }
    //任务列表
    @RequiresPermissions("cms:task:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Task task){
        startPage();
        List<Task> list = taskService.selectTaskList(task);
        for (Task t:list) {
            System.out.println(t.toString());
        }
        return getDataTable(list);
    }
    //输出表单
    @Log(title = "任务管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("cms:task:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Task task)
    {
        List<Task> list =taskService.selectTaskList(task);
        ExcelUtil<Task> util = new ExcelUtil<Task>(Task.class);
        return util.exportExcel(list, "任务数据");
    }
    //导入表单
    @Log(title = "任务管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("cms:task:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Task> util = new ExcelUtil<Task>(Task.class);
        List<Task> taskList = util.importExcel(file.getInputStream());
        String message = taskService.importTask(taskList, updateSupport);
        return AjaxResult.success(message);
    }
    @RequiresPermissions("cms:task:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<Task> util = new ExcelUtil<Task>(Task.class);
        return util.importTemplateExcel("任务数据");
    }
    /**
     * 新增任务
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        return prefix + "/add";
    }
    /**
     * 新增保存任务
     */
    @RequiresPermissions("cms:task:add")
    @Log(title = "任务管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Task task)
    {
        System.out.println("是否同意:"+task.getAgreeornot());
        System.out.println("task对象"+task.toString());
        if (taskService.selectTaskBytaskTitle(task.getTitle())!=null)
        {
            return error("新增任务'" + task.getTitle() + "'失败，该任务标题已存在");
        }
        else if (task.getNumber()>taskService.selectgoodsCount(task.getName()))
        {
            return error("新增任务'" + task.getNumber() + "'失败，任务数量超出库存已存在");
        }
        return toAjax(taskService.insertTask(task));
    }

    /**
     * 修改任务
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        mmap.put("cmsTask", taskService.selectTaskById(id));
        return prefix + "/edit";
    }

    /**
     * 修改保存用户
     */
    @RequiresPermissions("cms:task:edit")
    @Log(title = "任务管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Task task)
    {
        if (taskService.selectTaskBytaskTitle(task.getTitle())!=null)
        {
            return error("修改任务'" + task.getTitle() + "'失败，该任务标题已存在");
        }else if(task.getNumber()>taskService.selectgoodsCount(task.getName())){
            return error("修改任务"+task.getNumber()+"失败，该次任务数量大于仓库库存数量");
        }
        return toAjax(taskService.updateTask(task));
    }
    @RequiresPermissions("cms:task:remove")
    @Log(title = "任务管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Integer ids)
    {
        try
        {
            System.out.println(ids);
            return toAjax(taskService.deleteTaskById(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
}
